// import { merchCoinList, appList, merchConfig, merchPermission } from '@/api/basicSettings'
import { my_info } from '@/api/user'
// import _enum from '@/utils/enumKey'
const MY_INFO = 'MY_INFO'
const USER = 'user'

const getDefaultState = () => {
  return {
    myInfo: {},
    name: ''
  }
}
const state = {
  myInfo: localStorage.getItem(MY_INFO) ? JSON.parse(localStorage.getItem(MY_INFO)) : {},
  name: ''
}

const mutations = {
  SET_MY_INFO: (state, myInfo) => {
    state.myInfo = myInfo
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  async getMyInfo({ commit }, param) {
    return new Promise((resolve, reject) => {
      my_info(param).then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        // 兼容webase
        localStorage.setItem(USER, data.account)
        localStorage.setItem(MY_INFO, JSON.stringify(data))
        commit('SET_MY_INFO', data)
        commit('SET_NAME', data.account)
        resolve(data.menu.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  localStorageCleaner({ commit }) {
    localStorage.removeItem(MY_INFO)
    localStorage.removeItem(USER)
    // webase缓存清除
    localStorage.removeItem('groupName')
    localStorage.removeItem('encryptionId')
    localStorage.removeItem('versionId')
    localStorage.removeItem('groupId')
    localStorage.removeItem('selectData')
    localStorage.removeItem('contractList')
    localStorage.removeItem('isFinishCompile')
    commit('RESET_STATE')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

